<template>
  <van-icon
    :color="value === 1 ? '#e5645f' : '#777'"
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    @click="onCollect"
  />
</template>

<script>
import { addLike, removeLike } from '@/api/articles'
export default {
  name: 'LikeArticle',
  components: {

  },
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, Object, String],
      required: true
    }
  },
  data () {
    return {

    }
  },
  created () {

  },
  methods: {
    async onCollect () {
      if (this.value === 1) {
        // 取消点赞
        await removeLike(this.articleId)
      } else {
        // 点赞
        await addLike(this.articleId)
      }
      // 如果 value === 1 说明走的是取消点赞分支,所以将状态值修改为 -1
      // 如果 value !== 1 说明走的是点赞分支,所以将状态值修改为 1
      this.$emit('input', this.value === 1 ? -1 : 1)
    }
  }
}
</script>

<style lang="less" scoped>

</style>
